<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <title>空间三角形</title>
    <link href="../Build/Cesium/Widgets/widgets.css" rel="stylesheet" />
    <script src="../Build/Cesium/Cesium.js"></script>

    <script src="plugin/PrimitiveTriangles.js"></script>
    <style>
        html,
        body,
        #cesiumContainer {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>

</head>

<body>
    <div id="cesiumContainer"></div>
    <script>
        let osm = Cesium.createOpenStreetMapImageryProvider({
            url: 'https://a.tile.openstreetmap.org/'
        });
        let viewer = new Cesium.Viewer('cesiumContainer', {
            imageryProvider: osm,
            contextOptions: {
                webgl: {
                    alpha: true
                }
            },
            selectionIndicator: false,
            animation: false,  //是否显示动画控件
            baseLayerPicker: false, //是否显示图层选择控件
            geocoder: false, //是否显示地名查找控件
            timeline: false, //是否显示时间线控件
            sceneModePicker: false, //是否显示投影方式控件
            navigationHelpButton: false, //是否显示帮助信息控件
            infoBox: false,  //是否显示点击要素之后显示的信息
            fullscreenButton: false
        });
        //取消双击事件
        viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
        //设置homebutton的位置
        Cesium.Camera.DEFAULT_VIEW_RECTANGLE =
            Cesium.Rectangle.fromDegrees(110.15, 34.54, 110.25, 34.56);//Rectangle(west, south, east, north)
        //设置初始位置
        viewer.camera.setView({
            destination: Cesium.Cartesian3.fromDegrees(110.20, 34.55, 30000)
        });

        //开启深度检测
        //viewer.scene.globe.depthTestAgainstTerrain = true;

        let primitiveTriangles=null;
        //画立体扇形
        /*
        @center 中心点
        @radius 半径
        @rotate 旋转角度（正东为0，顺时针为正方向）
        @angle 扇形角度
        */
        function drawSector(center, radius, rotate, angle) {
            let vertex = [];
            let ellipsoid = viewer.scene.globe.ellipsoid;
            if (angle > 180) angle = 180;
            for (let i = 0; i < angle; i++) {
                if (i >= 90) {
                    let bx = -radius / 111201 * Math.cos(i * Math.PI / 180);
                    let bz = radius * Math.sin(i * Math.PI / 180);
                    let bxx = bx * Math.cos(rotate * Math.PI / 180);
                    let byy = bx * Math.sin(rotate * Math.PI / 180);
                    let tx = -radius / 111201 * Math.cos((i + 1) * Math.PI / 180);
                    let tz = radius * Math.sin((i + 1) * Math.PI / 180);
                    let txx = tx * Math.cos(rotate * Math.PI / 180);
                    let tyy = tx * Math.sin(rotate * Math.PI / 180);
                    vertex.push(Cesium.Cartesian3.fromDegrees(center.x, center.y, 0))
                    vertex.push(Cesium.Cartesian3.fromDegrees(center.x - bxx, center.y - byy, bz))
                    vertex.push(Cesium.Cartesian3.fromDegrees(center.x - txx, center.y - tyy, tz))
                } else {
                    let bx = radius / 111201 * Math.cos(i * Math.PI / 180);
                    let bz = radius * Math.sin(i * Math.PI / 180);
                    let bxx = bx * Math.cos(rotate * Math.PI / 180);
                    let byy = bx * Math.sin(rotate * Math.PI / 180);
                    let tx = radius / 111201 * Math.cos((i + 1) * Math.PI / 180);
                    let tz = radius * Math.sin((i + 1) * Math.PI / 180);
                    let txx = tx * Math.cos(rotate * Math.PI / 180);
                    let tyy = tx * Math.sin(rotate * Math.PI / 180);
                    vertex.push(Cesium.Cartesian3.fromDegrees(center.x, center.y, 0))
                    vertex.push(Cesium.Cartesian3.fromDegrees(center.x + bxx, center.y + byy, bz))
                    vertex.push(Cesium.Cartesian3.fromDegrees(center.x + txx, center.y + tyy, tz))
                }
            }
            if(!primitiveTriangles){
                primitiveTriangles = new PrimitiveTriangles({ 'viewer': viewer, 'Cartesians': vertex, 'Colors': [0, 1, 1, 0.5, 0, 0, 1, 0.5, 0, 1, 1, 0.5] });
            }else{
                primitiveTriangles.updateCartesianPositionColor(vertex, [0, 1, 1, 0.5, 0, 0, 1, 0.5, 0, 1, 1, 0.5]);
            }
        }

        let index=0;
        setInterval(() => {
            if(index>45){
                index=0;
            }
            drawSector({
                x: 110.20,
                y: 34.55
            }, 10000, index, 45);
            index++;
        }, 50)
    </script>
</body>

</html>